﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace FCalendar
{
    public partial class MainForm : Form
    {
        public bool showBox;
        public bool showForm;

        public MainForm()
        {
            InitializeComponent();
            ShowDate();
            showForm = true;
            showBox = false;
        }

        public string[] fmonthNames = new string[] {
            "فروردین",
            "اردیبهشت",
            "خرداد",
            "تیر",
            "مرداد",
            "شهریور",
            "مهر",
            "آبان",
            "آذر",
            "دی",
            "بهمن",
            "اسفند"
        };

        public string[] fdayNames = new string[] {
            "یکشنبه",
            "دوشنبه",
            "سه شنبه",
            "چهارشنبه",
            "پنج شنبه",
            "جمعه",
            "شنبه"
        };


        public void ShowDate()
        {
            DateTime gt = monthCalendar1.SelectionStart;
            PersianCalendar pc = new PersianCalendar();
            int year = pc.GetYear(gt);
            int month = pc.GetMonth(gt);
            int day = pc.GetDayOfMonth(gt);
            int dow = (int)pc.GetDayOfWeek(gt);
            string str;
            str = fdayNames[dow] + " " + day.ToString()+ " " + fmonthNames[month-1] + " " + year.ToString();
            // Set Notify Texts
            notifyIcon1.Text = str;
            notifyIcon1.Icon = CreateDayIcon(month, day);
            // Set Box Text
            if (Program.fbox != null)
            {
                Program.fbox.boxText = str;
                Program.fbox.Invalidate();
            }
        }

        Bitmap notifyBitmap = new Bitmap(16, 16);

        protected Icon CreateDayIcon(int month, int day)
        {
            Graphics notifyGraphic = Graphics.FromImage(notifyBitmap);
            notifyGraphic.Clear(Color.Transparent);
            notifyGraphic.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
            using (Brush brush1 = new SolidBrush(Program.foption.colorPickerMonth.SelectedColor))
            using (Brush brush2 = new SolidBrush(Program.foption.colorPickerDay.SelectedColor))
            using (Font font1 = new Font("Tahoma", 6, FontStyle.Bold))
            {
                notifyGraphic.DrawString(month.ToString(), font1, brush1,
                    new RectangleF(0, 0, 16, 8));
                notifyGraphic.DrawString(day.ToString(), font1, brush2,
                    new RectangleF(0, 8, 16, 8));
            }
            return Icon.FromHandle(notifyBitmap.GetHicon());
        }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            ShowDate();
            farsiCalendarControl1.SelectedGeoValue = e.Start;
        }

        public void ShowCalendarForm()
        {
            showForm = true;
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ShowCalendarForm();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            ShowCalendarForm();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Hide();
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            notifyIcon1.Icon = null;
            FCalendar.Properties.Settings.Default.ShowBox = Program.mainForm.boxToolStripMenuItem.Checked;
            FCalendar.Properties.Settings.Default.Save();
            Application.ExitThread();
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            Program.foption.Show();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                Hide();
                e.Cancel = true;
            }
            FCalendar.Properties.Settings.Default.ShowBox = Program.mainForm.boxToolStripMenuItem.Checked;
            FCalendar.Properties.Settings.Default.Save();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Program.IsToday)
            {
                monthCalendar1.SetDate(DateTime.Now);
                ShowDate();
                // Set timer for next day
                TimeSpan t = DateTime.Today.AddDays(1) - DateTime.Now;
                timer1.Interval = (int)t.TotalMilliseconds;
            }
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            Program.IsToday = !Program.IsToday;
            if (Program.IsToday)
                monthCalendar1.SetDate(DateTime.Now);
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            if (!showForm)
                Hide();
            if (showBox)
                Program.fbox.Show();
            // Set best size
            int margin = 40;
            this.MinimumSize = new Size(monthCalendar1.SingleMonthSize.Width + margin, this.MinimumSize.Height);
            this.Width = monthCalendar1.SingleMonthSize.Width + margin;
        }

        private void boxToolStripMenuItem_Click(object sender, EventArgs e)
        {
            boxToolStripMenuItem.Checked = !boxToolStripMenuItem.Checked;
            if (boxToolStripMenuItem.Checked)
            {
                Program.fbox.Show();
                ShowDate();
            }
            else
            {
                Program.fbox.Hide();
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            FCalendar.Properties.Settings.Default.Save();
        }

        private void farsiCalendarControl1_DateChanged(object sender)
        {
            monthCalendar1.SelectionStart = farsiCalendarControl1.SelectedGeoValue;
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About af = new About();
            af.ShowDialog(this);
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            // Set the geo calendar position
            int w = monthCalendar1.Width / 2;
            monthCalendar1.Left = (this.ClientRectangle.Width / 2) - w;

        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            
        }

        private void farsiCalendarControl1_SelectDay(object sender)
        {
            Program.IsToday = false;
            toolStripMenuItem5.Checked = false;
        }

        private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
        {
            Program.IsToday = false;
        }

    }
}